<template>
  <a-card :bordered="false">
    <div class="table-page-search-wrapper">
      <a-form layout="inline" @keyup.enter.native="searchQuery">
        <a-row :gutter="24">
          <a-col :xl="10" :lg="11" :md="12" :sm="24">
            <a-form-item :label="$t('时间')">
              <a-date-picker
                :show-time="true"
                valueFormat="YYYY-MM-DD HH:mm:ss"
                :placeholder="$t('请选择开始时间')"
                class="query-group-cust"
                v-model="queryParam.createTime_begin"
                style="min-width: calc(50% - 10px)"
              ></a-date-picker>
              <span class="query-group-split-cust"></span>
              <a-date-picker
                :show-time="true"
                valueFormat="YYYY-MM-DD HH:mm:ss"
                :placeholder="$t('请选择结束时间')"
                class="query-group-cust"
                v-model="queryParam.createTime_end"
                style="min-width: calc(50% - 10px)"
              ></a-date-picker>
            </a-form-item>
          </a-col>
          <a-col :xl="5" :lg="6" :md="7" :sm="24">
            <a-form-item :label="$t('项目分类')">
              <a-tree-select
                allowClear
                v-model="queryParam.gameTypeId"
                style="width: 100%"
                :dropdownStyle="{ maxHeight: '400px', overflow: 'auto' }"
                :tree-data="gameTypeList"
                :search-placeholder="$t('请选择')"
              />
            </a-form-item>
          </a-col>
          <template v-if="toggleSearchStatus">
            <a-col :xl="5" :lg="6" :md="7" :sm="24">
              <a-form-item :label="$t('项目名称')">
                <a-select v-model="queryParam.gameId">
                  <a-select-option :value="item.id" :key="index" v-for="(item, index) in gameList">{{
                    item.name
                  }}</a-select-option>
                </a-select>
              </a-form-item>
            </a-col>
            <a-col :xl="5" :lg="6" :md="7" :sm="24">
              <a-form-item :label="$t('会员')">
                <a-input v-model="queryParam.memberInfo" :placeholder="$t('请输入会员手机号/卡号/姓名')"></a-input>
              </a-form-item>
            </a-col>
            <a-col :xl="5" :lg="6" :md="7" :sm="24">
              <a-form-item :label="$t('机号')">
                <a-input v-model="queryParam.machineNo" :placeholder="$t('请输入机号')"></a-input>
              </a-form-item>
            </a-col>
            <a-col :xl="5" :lg="6" :md="7" :sm="24">
              <a-form-item :label="$t('汇总方式')">
                <a-select v-model="queryParam.groupBy">
                  <a-select-option value="game_type_id,game_id,device_id,machine_no,member_id">{{ $t('会员机位消费') }}</a-select-option>
                  <a-select-option value="member_id">{{ $t('会员消费') }}</a-select-option>
                </a-select>
              </a-form-item>
            </a-col>
          </template>
          <a-col :xl="6" :lg="7" :md="8" :sm="24">
            <span style="float: left; overflow: hidden" class="table-page-search-submitButtons">
              <a-button type="primary" @click="searchQuery" icon="search">{{ $t('查询') }}</a-button>
              <a-button type="primary" @click="searchReset" icon="reload" style="margin-left: 8px">{{
                $t('重置')
              }}</a-button>
              <a @click="handleToggleSearch" style="margin-left: 8px">
                {{ toggleSearchStatus ? $t('收起') : $t('展开') }}
                <a-icon :type="toggleSearchStatus ? 'up' : 'down'" />
              </a>
              <a-button
              v-has="'BisGameCoinRecordController:export'"
                type="primary"
                :loading="exloading"
                style="margin-left: 8px"
                @click="handleExportXls($t('会员机位消费'))"
                >{{ $t('导出') }}</a-button
              >
            </span>
          </a-col>
        </a-row>
      </a-form>
      <a-table
        ref="table"
        size="middle"
        bordered
        rowKey="id"
        class="j-table-force-nowrap"
        :columns="columns"
        :dataSource="dataSource"
        :pagination="ipagination"
        :customHeaderRow="customHeaderRow"
        :customRow="customRow"
        @change="handleTableChange"
      >
        <template slot="footer">
          <div style="font-weight: bold">
            <span
              >{{ $t('支付金额') }}：{{
                totalInfo && totalInfo.payAmount ? totalInfo.payAmount.toFixed(2) : '0.00'
              }}</span
            >
            <span v-if="operateCoin == 1" style="margin-left: 12px"
              >{{ $t('投币') }}：{{
                totalInfo && totalInfo.memberCoin ? totalInfo.memberCoin.toFixed(2) : '0.00'
              }}</span
            >
            <span  v-if="operateALottery == 1" style="margin-left: 12px"
              >{{ $t('投A票') }}：{{
                totalInfo && totalInfo.memberAlottery ? totalInfo.memberAlottery.toFixed(2) : '0.00'
              }}</span
            >
            <span  v-if="operateACoin == 1" style="margin-left: 12px"
              >{{ $t('投Z票') }}：{{
                totalInfo && totalInfo.memberZlottery ? totalInfo.memberZlottery.toFixed(2) : '0.00'
              }}</span
            >
            <span  v-if="operateLottery == 1" style="margin-left: 12px"
              >{{ $t('投彩票') }}：{{
                totalInfo && totalInfo.memberLottery ? totalInfo.memberLottery.toFixed(2) : '0.00'
              }}</span
            >
            <span style="margin-left: 12px"
              >{{ $t('投实币') }}：{{
                totalInfo && totalInfo.physicalCoin ? totalInfo.physicalCoin.toFixed(2) : '0.00'
              }}</span
            >
            <span style="margin-left: 12px"
              >{{ $t('总投币') }}：{{
                totalInfo && totalInfo.totalCoin ? totalInfo.totalCoin.toFixed(2) : '0.00'
              }}</span
            >
            <span  v-if="operateLottery == 1" style="margin-left: 12px"
              >{{ $t('退彩票') }}：{{
                totalInfo && totalInfo.outputLottery ? totalInfo.outputLottery.toFixed(2) : '0.00'
              }}</span
            >
            <span  v-if="operateALottery == 1" style="margin-left: 12px"
              >{{ $t('退A票') }}：{{
                totalInfo && totalInfo.outputAlottery ? totalInfo.outputAlottery.toFixed(2) : '0.00'
              }}</span
            >
            <span  v-if="operateACoin == 1" style="margin-left: 12px"
              >{{ $t('退Z票') }}：{{
                totalInfo && totalInfo.outputZlottery ? totalInfo.outputZlottery.toFixed(2) : '0.00'
              }}</span
            >
            <span  v-if="operateCoin == 1" style="margin-left: 12px"
              >{{ $t('退币') }}：{{
                totalInfo && totalInfo.outputCoin ? totalInfo.outputCoin.toFixed(2) : '0.00'
              }}</span
            >
            <span style="margin-left: 12px"
              >{{ $t('总退票') }}：{{
                totalInfo && totalInfo.outputValue ? totalInfo.outputValue.toFixed(2) : '0.00'
              }}</span
            >
            <span style="margin-left: 12px"
              >{{ $t('退礼品数') }}：{{ totalInfo && totalInfo.goodsNumber ? totalInfo.goodsNumber : '0' }}</span
            >
            <span style="margin-left: 12px"
              >{{ $t('投入金额') }}：{{
                totalInfo && totalInfo.inputPrice ? totalInfo.inputPrice.toFixed(2) : '0.00'
              }}</span
            >
            <span style="margin-left: 12px"
              >{{ $t('退出金额') }}：{{
                totalInfo && totalInfo.outputPrice ? totalInfo.outputPrice.toFixed(2) : '0.00'
              }}</span
            >
            <span style="margin-left: 12px"
              >{{ $t('毛利润') }}：{{ totalInfo && totalInfo.profit ? totalInfo.profit.toFixed(2) : '0.00' }}</span
            >
            <span style="margin-left: 12px"
              >{{ $t('毛利率') }}：{{
                totalInfo && totalInfo.profitRate ? totalInfo.profitRate.toFixed(2) + '%' : '0.00%'
              }}</span
            >
          </div>
        </template>
      </a-table>
    </div>
  </a-card>
</template>
<script>
import moment from 'moment'
import '@/assets/less/TableExpand.less'
import { mixinDevice } from '@/utils/mixin'
import { JeecgListMixin } from '@/mixins/JeecgListMixin'
import { getAction,downFile } from '@/api/manage'
export default {
  name: 'MemberClientConsume',
  mixins: [JeecgListMixin, mixinDevice],
  components: {},
  data() {
    return {
      description: '会员机位消费',
      moment,
      defColumns: [
        {
          title: this.$t('手机号'),
          align: 'center',
          dataIndex: 'memberPhone',
        },
        {
          title: this.$t('卡号'),
          align: 'center',
          dataIndex: 'chipNo',
        },
        {
          title: this.$t('姓名'),
          align: 'center',
          dataIndex: 'memberName',
        },
        {
          title: this.$t('支付金额'),
          align: 'center',
          sorter: true,
          dataIndex: 'payAmount',
          customRender: function (t, r) {
            return t.toFixed(2)
          },
          customCell: function (t, r) {
            return {
              style: {
                textAlign: 'right',
              },
            }
          },
        },
        {
          title: this.$t('投实币'),
          align: 'center',
          sorter: true,
          dataIndex: 'physicalCoin',
          customRender: function (t, r) {
            return t.toFixed(2)
          },
          customCell: function (t, r) {
            return {
              style: {
                textAlign: 'right',
              },
            }
          },
        },
        {
          title: this.$t('总投币'),
          align: 'center',
          sorter: true,
          dataIndex: 'totalCoin',
          customRender: function (t, r) {
            if (t != null) {
              return t.toFixed(2)
            }
          },
          customCell: function (t, r) {
            return {
              style: {
                textAlign: 'right',
              },
            }
          },
        },
        {
          title: this.$t('总退票'),
          align: 'center',
          sorter: true,
          dataIndex: 'outputValue',
          customRender: function (t, r) {
            if(!t){
              t=0
            }
            return t.toFixed(2)
          },
          customCell: function (t, r) {
            return {
              style: {
                textAlign: 'right',
              },
            }
          },
        },
        {
          title: this.$t('礼品名称'),
          align: 'center',
          dataIndex: 'outputGoodsId_dictText',
        },
        {
          title: this.$t('退礼品数'),
          align: 'center',
          sorter: true,
          dataIndex: 'goodsNumber',
          customCell: function (t, r) {
            return {
              style: {
                textAlign: 'right',
              },
            }
          },
        },
        {
          title: this.$t('投入金额'),
          align: 'center',
          sorter: true,
          dataIndex: 'inputPrice',
          customRender: function (t, r) {
            return t.toFixed(2)
          },
          customCell: function (t, r) {
            return {
              style: {
                textAlign: 'right',
              },
            }
          },
        },
        {
          title: this.$t('退出金额'),
          align: 'center',
          sorter: true,
          dataIndex: 'outputPrice',
          customRender: function (t, r) {
            return t.toFixed(2)
          },
          customCell: function (t, r) {
            return {
              style: {
                textAlign: 'right',
              },
            }
          },
        },
        {
          title: this.$t('毛利润'),
          align: 'center',
          sorter: true,
          dataIndex: 'profit',
          customRender: function (t, r) {
            return t.toFixed(2)
          },
          customCell: function (t, r) {
            return {
              style: {
                textAlign: 'right',
              },
            }
          },
        },
        {
          title: this.$t('毛利率'),
          align: 'center',
          dataIndex: 'profitRate',
          customRender: function (t, r) {
            return t.toFixed(2) + '%'
          },
          customCell: function (t, r) {
            return {
              style: {
                textAlign: 'right',
              },
            }
          },
        },
      ],
      columns:[],
      columns2:[],
      columns3:[],
      hideColumns:'',
      operateCoin:0,
      operateLottery:0,
      operateACoin:0,
      operateALottery:0,
      gameTypeList: [],
      gameList: [],
      url: {
        list: '/game/bisGameCoinRecord/getReport',
        exportXlsUrl: '/game/bisGameCoinRecord/exportReportXls',
      },
      queryParam: {
        createTime_begin: moment().format('YYYY-MM-DD 00:00:00'),
        createTime_end: moment().format('YYYY-MM-DD 23:59:59'),
        gameTypeId: '',
        gameId: '',
        groupBy: 'game_type_id,game_id,device_id,machine_no,member_id',
      },
      totalCallback: this.totalCallbacks,
      isCustom: true,
    }
    
  },
  methods: {
    totalCallbacks(totalInfo) {
      this.totalInfo = totalInfo
    },
    initDictConfig() {
      this.initColumns()
      this.getGameTypes()
      getAction('/game/bisGame/list', { pageSize: 999 }).then((res) => {
        if (res.success) {
          this.gameList = res.result.records
          this.gameList.unshift({ id: '', name: this.$t('全部') })
        }
      })
    },
    initColumns() {
      this.columns=JSON.parse(JSON.stringify(this.defColumns)) ;
      this.columns2=[];
      this.columns3=[];
      this.columns4=[];
      this.hideColumns='';
      this.operateCoin=localStorage.getItem('operateCoin');
      if (localStorage.getItem('operateCoin') == 1) {
        this.columns2.splice(this.columns2.length, 0, {
          title: this.$t('投币'),
          align: 'center',
          sorter: true,
          dataIndex: 'memberCoin',
          customRender: function (t, r) {
            if(!t){
              t=0
            }
            return t.toFixed(2)
          },
          customCell: function (t, r) {
            return {
              style: {
                textAlign: 'right',
              },
            }
          },
        });
        this.columns3.splice(this.columns3.length, 0, {
          title: this.$t('退币'),
          align: 'center',
          sorter: true,
          dataIndex: 'outputCoin',
          customRender: function (t, r) {
            if(!t){
              t=0
            }
            return t.toFixed(2)
          },
          customCell: function (t, r) {
            return {
              style: {
                textAlign: 'right',
              },
            }
          },
        })
      }
      else{
        this.hideColumns+='memberCoin,outputCoin,';
      }
      this.operateLottery=localStorage.getItem('operateLottery');
      if (localStorage.getItem('operateLottery') == 1) {
        this.columns2.splice(this.columns2.length, 0, {
          title: this.$t('投彩票'),
          align: 'center',
          sorter: true,
          dataIndex: 'memberLottery',
          customRender: function (t, r) {
            if(!t){
              t=0
            }
            return t.toFixed(2)
          },
          customCell: function (t, r) {
            return {
              style: {
                textAlign: 'right',
              },
            }
          },
        });
        this.columns3.splice(this.columns3.length, 0, {
          title: this.$t('退彩票'),
          align: 'center',
          sorter: true,
          dataIndex: 'outputLottery',
          customRender: function (t, r) {
            if(!t){
              t=0
            }
            return t.toFixed(2)
          },
          customCell: function (t, r) {
            return {
              style: {
                textAlign: 'right',
              },
            }
          },
        })
      }else{
        this.hideColumns+='memberLottery,outputLottery,';
      }
      this.operateALottery=localStorage.getItem('operateALottery');
      if (localStorage.getItem('operateALottery') == 1) {
        this.columns2.splice(this.columns2.length, 0, {
          title: this.$t('投A票'),
          align: 'center',
          sorter: true,
          dataIndex: 'memberAlottery',
          customRender: function (t, r) {
            if(!t){
              t=0
            }
            return t.toFixed(2)
          },
          customCell: function (t, r) {
            return {
              style: {
                textAlign: 'right',
              },
            }
          },
        });
        this.columns3.splice(this.columns3.length, 0, {
          title: this.$t('退A票'),
          align: 'center',
          sorter: true,
          dataIndex: 'outputAlottery',
          customRender: function (t, r) {
            if(!t){
              t=0
            }
            return t.toFixed(2)
          },
          customCell: function (t, r) {
            return {
              style: {
                textAlign: 'right',
              },
            }
          },
        })
      }else{
        this.hideColumns+='memberAlottery,outputAlottery,';
      }
      this.operateACoin=localStorage.getItem('operateACoin');
      if (localStorage.getItem('operateACoin') == 1) {
        this.columns2.splice(this.columns2.length, 0, {
          title: this.$t('投Z票'),
          align: 'center',
          sorter: true,
          dataIndex: 'memberZlottery',
          customRender: function (t, r) {
            if(!t){
              t=0
            }
            return t.toFixed(2)
          },
          customCell: function (t, r) {
            return {
              style: {
                textAlign: 'right',
              },
            }
          },
        });
        this.columns3.splice(this.columns3.length, 0, {
          title: this.$t('退Z票'),
          align: 'center',
          sorter: true,
          dataIndex: 'outputZlottery',
          customRender: function (t, r) {
            if(!t){
              t=0
            }
            return t.toFixed(2)
          },
          customCell: function (t, r) {
            return {
              style: {
                textAlign: 'right',
              },
            }
          },
        })
      }else{
        this.hideColumns+='memberZlottery,outputZlottery,';
      }
      if("member_id" != this.queryParam.groupBy){
        this.columns4.splice(this.columns4.length, 0, 
          {
            title: this.$t('项目分类'),
            align: 'center',
            dataIndex: 'gameTypeId_dictText',
          },
          {
            title: this.$t('项目名称'),
            align: 'center',
            dataIndex: 'gameId_dictText',
          },
          {
            title: this.$t('设备编码'),
            align: 'center',
            dataIndex: 'deviceId',
          },
          {
            title: this.$t('设备名称'),
            align: 'center',
            dataIndex: 'deviceId_dictText',
          },
          {
            title: this.$t('机号'),
            align: 'center',
            dataIndex: 'machineNo',
          }
        );
      } else {
        this.hideColumns+='gameTypeId_dictText,gameId_dictText,deviceId,deviceId_dictText,machineNo,';
      }
      //hideColumns如果长度大于0去掉最后一位
      if (this.hideColumns.length > 0) {
        this.hideColumns = this.hideColumns.substring(0, this.hideColumns.length - 1)
      }
      this.columns.splice(0, 0,...this.columns4);
      this.columns.splice(4+this.columns4.length, 0,...this.columns2);
      this.columns.splice(6+this.columns4.length+this.columns2.length, 0,...this.columns3);
      this.loadData(1)
    },
    searchQuery() {
      this.selectedRowIndex = []
      this.loadData(1);
      // 点击查询清空列表选中行
      // https://gitee.com/jeecg/jeecg-boot/issues/I4KTU1
      this.selectedRowKeys = []
      this.selectionRows = []
      this.initColumns()
    },
    searchReset() {
      this.queryParam = {
        createTime_begin: moment().format('YYYY-MM-DD 00:00:00'),
        createTime_end: moment().format('YYYY-MM-DD 23:59:59'),
        gameTypeId: '',
        gameId: '',
        groupBy: 'game_type_id,game_id,device_id,machine_no,member_id',
      }
      this.searchQuery()
    },
    getGameTypes() {
      let that = this
      let gameTypeList = []
      let param = {}
      param.pageNo = 1
      param.pageSize = 9999
      param.column = 'id'
      param.order = 'DESC'
      getAction('/game/bisGameType/rootList', param).then((res) => {
        if (res.success) {
          gameTypeList = res.result.records
          if (gameTypeList.length > 0) {
            gameTypeList.forEach((col) => {
              col.key = '' + col.id
              col.value = '' + col.id
              col.title = '' + col.name
              col.id = '' + col.id
              col.pid = '' + col.pid
              col.hasChild = '' + col.hasChild
              if (col.hasChild != '0') {
                that.getHasChildStore(col)
              }
            })
            that.gameTypeList = that.getDataByResult(gameTypeList)
            that.gameTypeList.unshift({ key: '', id: '', value: '', title: that.$t('全部') })
          }
        }
      })
    },
    getHasChildStore(type) {
      let that = this
      let query = {}
      query.pageNo = 1
      query.pageSize = 9999
      query.pid = type.id
      getAction('/game/bisGameType/childList', query).then((res) => {
        if (res.success) {
          if (res.result.records.length > 0) {
            res.result.records.forEach((col) => {
              col.key = '' + col.id
              col.value = '' + col.id
              col.title = '' + col.name
              col.pid = '' + col.pid
              col.id = '' + col.id
              col.hasChild = '' + col.hasChild
              if (col.hasChild != '0') {
                that.getHasChildStore(col)
              }
            })
            type.children = that.getDataByResult(res.result.records)
            that.gameTypeList = [...that.gameTypeList]
          }
        } else {
          that.$message.warning(res.message)
        }
      })
    },
    getDataByResult(result) {
      return result.map((item) => {
        return item
      })
    },
    handleExportXls(fileName) {
      this.exloading = true
  
      if (!fileName || typeof fileName != "string") {
        fileName = this.$t('导出文件')
      }
      let param = this.getQueryParams();
      console.log("导出参数", this.columns)
      param.hideColumns = this.hideColumns;//this.getColumnField('columns','defColumns')
      console.log("导出参数", param)
      downFile(this.url.exportXlsUrl, param).then((data) => {
        if (!data) {
          this.$message.warning(this.$t('文件下载失败'))
          return
        }
        if (typeof window.navigator.msSaveBlob !== 'undefined') {
          window.navigator.msSaveBlob(new Blob([data], { type: 'application/vnd.ms-excel' }), fileName + '.xls')
        } else {
          let url = window.URL.createObjectURL(new Blob([data], { type: 'application/vnd.ms-excel' }))
          let link = document.createElement('a')
          link.style.display = 'none'
          link.href = url
          link.setAttribute('download', fileName + '.xls')
          document.body.appendChild(link)
          link.click()
          document.body.removeChild(link); //下载完成移除元素
          window.URL.revokeObjectURL(url); //释放掉blob对象
        }
      }).finally(() => {
        this.exloading = false
      })
    },
  },
}
</script>